// ANES24Ideology
log using ANES24Ideology.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\anes_timeseries_2024_stata_20250219.dta" // ANES 2024 Time Series Study // Preliminary Release: Pre-Election Data February 19, 2025 version

// Create social justice scale
* Rename variables 
rename V241290x Edi 
rename V241372x Tgbathroom 
rename V241375x Tgsport 
rename V241412x Appprotestgaza 

* Delete missing values
foreach var in Edi Tgbathroom Tgsport Appprotestgaza {
    replace `var' = . if `var' < 0
    tabulate `var', missing
}

*Reverse coding so social justice values are high
foreach var in Appprotestgaza Tgbathroom Edi {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rEdi rTgbathroom Tgsport rAppprotestgaza {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

*At this point, the scale has a Cronbach's alpha of 0.76. 

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in srEdi srTgbathroom sTgsport srAppprotestgaza {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in srEdi srTgbathroom sTgsport srAppprotestgaza  {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Liberalism scale
* Rename
rename V241269x bordersecurity
rename V241302 abortion
rename V241272x crime
rename V241308x deathpenalty
rename V241395x wall
rename V241389x birthrightcit
rename V241381x gayadoption
rename V241258 environment

*Drop missing values
foreach var in gayadoption bordersecurity crime deathpenalty wall birthrightcit {
    replace `var' = . if inlist(`var', -1, -2)
}

replace abortion = . if inlist(abortion, -9, -8, -1, 5)   
replace environment = . if inlist(environment, -9, -8, -1, 99)  

*Reverse code so liberal values are high
foreach var in gayadoption environment {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}
 
*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in bordersecurity abortion crime deathpenalty wall birthrightcit rgayadoption renvironment {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in sbordersecurity sabortion scrime sdeathpenalty swall sbirthrightcit srgayadoption srenvironment {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreAL = 0
gen count_nonzeroAL = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in sbordersecurity sabortion scrime sdeathpenalty swall sbirthrightcit srgayadoption srenvironment {
    replace total_scoreAL = total_scoreAL + `var'
    replace count_nonzeroAL = count_nonzeroAL + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen LibValues = .
replace LibValues = total_scoreAL / count_nonzeroAL if count_nonzeroAL > 0

// Create left-wing scale
*Rename
rename V241263x Socsec 
rename V241266x Pubsch  
rename V241275x Welpro  
rename V241281x Aidpoor  
rename V241369x Parlea 

*Drop missing values
foreach var in Socsec Pubsch Welpro Aidpoor Parlea {
    replace `var' = . if `var' < 0
}

*Reverse coding so left values are high
foreach var in Socsec Pubsch Welpro Aidpoor Parlea {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rSocsec rPubsch rWelpro rAidpoor rParlea  {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values
foreach var in srSocsec srPubsch srWelpro srAidpoor srParlea {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreLR = 0
gen count_nonzeroLR = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in srSocsec srPubsch srWelpro srAidpoor srParlea {
    replace total_scoreLR = total_scoreLR + `var'
    replace count_nonzeroLR = count_nonzeroLR + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen LRScale = .
replace LRScale = total_scoreLR / count_nonzeroLR if count_nonzeroLR > 0

//SelfIDscale and Demfeeling
*Delete missing values
replace V241177=. if V241177==99 
replace V241177=. if V241177<0
replace V241166=. if V241166<0 

*Reverse so liberalselfid is high
egen maxval = max(V241177)
gen Selfid = maxval + 1 - V241177
drop maxval

rename V241166 Demfeeling

// Correlations
pwcorr SocJusValues LRScale LibValues Selfid Demfeeling [aweight=V240105a], sig

// Ideological constraint
*Create graduate dummy
gen Graduate=.
replace Graduate=0 if inrange(V241465x, 1, 3)
replace Graduate=1 if inlist(V241465x, 4, 5)

// Correlations
pwcorr rEdi rTgbathroom Tgsport rHumaidpal rAppprotestgaza [aweight=V240105a], sig
pwcorr rEdi rTgbathroom Tgsport rHumaidpal rAppprotestgaza if V241004==1 [aweight=V240105a], sig 
pwcorr rEdi rTgbathroom Tgsport rHumaidpal rAppprotestgaza if Graduate==1 [aweight=V240105a], sig 
pwcorr rEdi rTgbathroom Tgsport rHumaidpal rAppprotestgaza if Selfid>4 [aweight=V240105a], sig 
pwcorr rEdi rTgbathroom Tgsport rHumaidpal rAppprotestgaza if V241227x==1 [aweight=V240105a], sig 

log close
